﻿using System;
using System.Diagnostics;
using System.Threading;

namespace BookSourceVerification.utils;

public class NodeInfoStatistics
{
    // 可用书源
    public long AvailableCount;

    // 总数量
    public long TotalCount;
    private Stopwatch _stopwatch;

    public void Start()
    {
        this._stopwatch = new Stopwatch();
        this._stopwatch.Start();
    }

    /// <summary>
    /// 获得时间
    /// </summary>
    /// <returns>单位为毫秒</returns>
    public long GetTime()
    {
        return this._stopwatch.ElapsedMilliseconds / 1000;
    }

    /// <summary>
    /// 停止并获得时间
    /// </summary>
    /// <returns>单位为毫秒</returns>
    public long StopAndGetTime()
    {
        this._stopwatch.Stop();
        return this._stopwatch.ElapsedMilliseconds / 1000;
    }

    public void IncAvailableCount()
    {
        Interlocked.Add(ref this.AvailableCount, 1);
    }

    public void IncTotalCount()
    {
        Interlocked.Add(ref this.TotalCount, 1);
    }

    public (long AvailableCount, long TotalCount, long TotalTime) StopAndGetAllInfo()
    {
        try
        {
            this._stopwatch.Stop();
            return (this.AvailableCount, this.TotalCount, this._stopwatch.ElapsedMilliseconds / 1000);
        }
        finally
        {
            this.AvailableCount = 0;
            this.TotalCount = 0;
        }
    }

    public (long AvailableCount, long TotalCount, long TotalTime) GetAllInfo()
    {
        return (this.AvailableCount, this.TotalCount, this._stopwatch.ElapsedMilliseconds / 1000);
    }

}